package com.shaoshaohuo.app.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.utils.ToastUtil;

/**
 * 田贸e通项目fragment基类，所有fragment，应该直接继承此类
 * @author YinSF
 * @createDate 2015-11-15
 */
public class BaseFragment extends Fragment {


	private Dialog mDialog;
    private Handler handler;
    private Runnable runnable;

    @SuppressLint("ValidFragment")
    public BaseFragment() {
		super();
	}


	public void startLoadingDialog() {
    	if (handler == null) {
    		handler = new Handler();
    	}
    	if (runnable == null) {
    		runnable = new Runnable() {
    			
    			@Override
    			public void run() {
    				if (mDialog == null) {
    					mDialog = new Dialog(getContext(), R.style.dialog);
    					View contentView = LayoutInflater.from(getContext()).inflate(R.layout.layout_loadingdialog, null);
    					mDialog.setContentView(contentView);
    					Window window = mDialog.getWindow();
    					window.setWindowAnimations(R.style.customDialog_anim_style);
    					mDialog.setCanceledOnTouchOutside(false);
    					mDialog.setCancelable(true);
    				}
    				
    				mDialog.show();
    			}
    		};
    	}
    	handler.postDelayed(runnable, 1000);
    }

    public void dismissLoadingDialog() {
        if (handler != null && runnable != null) {
        	handler.removeCallbacks(runnable);
        	handler = null;
        	runnable = null;
        	if (mDialog != null) {
        		mDialog.dismiss();
        	}
        }
    }

	public void showToast(String text) {
        ToastUtil.toast(text);
    }
}
